﻿using System.Collections;
using System.Collections.Generic;
using LitJson;
using UnityEngine;
using UnityEngine.UI;

public class ChatListPanel : BasePanel {

	
	public Button chatListBtn;
	public Button friendListBtn;
	public Button myBtn;

	private GameObject _chatItemPrefab;

	private QueryChatListRequest _queryChatListRequest;
	// Use this for initialization
	void Start () {
		//组件初始化
		_chatItemPrefab = Resources.Load<GameObject>("UIPanel/ChatItem");
		_queryChatListRequest = this.gameObject.GetComponent<QueryChatListRequest>();
		
		chatListBtn.onClick.AddListener(delegate { ChatListButtonClicked(); });
		friendListBtn.onClick.AddListener(delegate { FriendListButtonClicked(); });
		myBtn.onClick.AddListener(delegate { MyButtonClicked(); });
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	
	
	
	private void ChatListButtonClicked()
	{	
		
		
		//  执行聊天列表请求拉取
		_queryChatListRequest.SendRequest();

	}


	private void FriendListButtonClicked()
	{
		UnityFacade.Instance.UiManager.PopPanel();

		UnityFacade.Instance.UiManager.PushPanel(PanelType.FriendListPanel);
	}


	private void MyButtonClicked()
	{
		UnityFacade.Instance.UiManager.PopPanel();

		UnityFacade.Instance.UiManager.PushPanel(PanelType.MyPanel);
		
	}


	public override void OnEnter()
	{
		if (!this.gameObject.activeSelf )
		{
			this.gameObject.SetActive(true);
		}
		//每次切进来的时候拉取新数据
		if (_queryChatListRequest == null)
		{
			_queryChatListRequest = this.gameObject.GetComponent<QueryChatListRequest>();
		}
		_queryChatListRequest.SendRequest();
	}

	public override void OnPause()
	{
		this.gameObject.SetActive(false);
	}

	public override void OnResume()
	{
		if (this.gameObject.activeSelf == false)
		{
			this.gameObject.SetActive(true);
		}
	}

	public override void OnExit()
	{
		this.gameObject.SetActive(false);
	}

	public void HandleResponse(JsonData respJsonData)
	{
		//移除现有的ChatItem
		GameObject[] chatItemGos = GameObject.FindGameObjectsWithTag("ChatItem");
		foreach (GameObject chatItemGo in chatItemGos)
		{
			Destroy(chatItemGo);
		}
		
		//获得挂载点
		Transform v_Layout_pos = UnityFacade.Instance.UiManager.InstantiatedPanelDict[PanelType.ChatListPanel].transform
			.Find("Panel_Center/V_Layout").transform;

		foreach (JsonData temp in respJsonData["data"])
		{
			int chatId = (int)temp["chatId"];
			int userId = (int) temp["userId"];
			string nick = temp["nick"].ToString();
			string avatar = temp["avatar"].ToString();
			string updateTime = temp["updateTime"].ToString();
			string historyMsg = temp["historyMsg"].ToString();//可能需要默认值(解析可能会出错)
			string type = temp["type"].ToString();
			//最近一次消息 todo historyMsg内容json格式尚未定义
//			if (!historyMsg.Equals(""))
//			{
//				JsonData historyMsgJson = JsonMapper.ToObject(historyMsg);
//				
//			}

			ChatVO chatVO = new ChatVO();
			chatVO.ChatId = chatId;
			chatVO.UserId = userId;
			chatVO.Nick = nick;
			chatVO.Avatar = avatar;
			chatVO.UpdateTime = updateTime;
			chatVO.HistoryMsg = historyMsg;
			chatVO.Type = type;

			GameObject chatItemGO = GameObject.Instantiate(_chatItemPrefab, v_Layout_pos, false);
			ChatItem chatItem = chatItemGO.GetComponent<ChatItem>();
			chatItem.ChatVo = chatVO;
			chatItem.SyncShow();


		}

	}
}
